<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
  <style>
    #member-app  .notAdmin::after{
      border: 0 !important;

    }
  </style>
</head>
<body>
<div class="dashboard-container" id="member-app">
  <div class="container">
    <div class="tableBar">
      <el-input
              v-model="input"
              placeholder="类型名"
              style="width: 250px"
              clearable
              @keyup.enter.native="handleTypeQuery"
      >
        <i
                slot="prefix"
                class="el-input__icon el-icon-search"
                style="cursor: pointer"
                @click="handleTypeQuery"
        ></i>
      </el-input>
      <el-button
              type="danger"
              @click="deleteTypeHandle('批量', null)"
      >
        + 批量删除
      </el-button>
      <el-button
              type="primary"
              @click="addTypeHandle('add')"
      >
        + 添加类型
      </el-button>
    </div>
    <el-table
            :data="tableData"
            stripe
            class="tableBox"
            @selection-change="handleSelectionChange"
    >
      <el-table-column
              type="selection"
              width="25"
      ></el-table-column>
      <el-table-column
              prop="id"
              label="ID"
      ></el-table-column>
      <el-table-column
              prop="type"
              label="类型名称"
      ></el-table-column>
      <el-table-column
              prop="description"
              label="类型描述"
      ></el-table-column>
      <el-table-column
              label="操作"
              width="160"
              align="center"
      >
        <template slot-scope="scope">
          <el-button
                  type="text"
                  size="small"
                  class="blueBug"
                  @click="addTypeHandle(scope.row.id)"
                  :class="{notAdmin:user !== 'admin'}"
          >
            编辑
          </el-button>
          <el-button
                  type="text"
                  size="small"
                  class="span-btn delBut non"
                  @click="deleteTypeHandle('',scope.row.id)"
                  :class="{notAdmin:user !== 'admin'}"
          >
            删除
          </el-button>
          <!--<el-button
            type="text"
            size="small"
            class="delBut non"
            @click="statusHandle(scope.row)"
            v-if="user === 'admin'"
          >
            {{ scope.row.status == '1' ? '禁用' : '启用' }}
          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
            class="pageList"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="counts"
            :current-page.sync="page"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/type.js"></script>
<script>
  new Vue({
    el: '#member-app',
    data() {
      return {
        input: '',
        counts: 0,
        page: 1,
        pageSize: 5,
        tableData : [],
        id : '',
        status : '',
      }
    },
    computed: {},
    created() {
      //这是生命周期函数 加载页面自动触发
      //然后就会自动调用init方法
      this.init()
      this.user = JSON.parse(localStorage.getItem('userInfo')).username
    },
    mounted() {
    },
    methods: {
      async init () {
        //发送请求所用的参数
        const params = {
          //当前页数
          page: this.page,
          //每页显示的多少条数据
          pageSize: this.pageSize,
          //根据名字搜索
          name: this.input ? this.input : undefined
        }
        //真正发送请求 获取数据
        await getTypePage(params).then(res => {
          if (String(res.code) === '1') {
            // 响应回来的数据给tableData赋值 进行页面渲染
            this.tableData = res.data.records || []
            //这是显示总共多少条数据
            this.counts = res.data.total
          }
        }).catch(err => {
          //出错就会触发这个回调方法
          this.$message.error('请求出错了：' + err)
        })
      },
      handleTypeQuery() {
        this.page = 1;
        this.init();
      },
      // 添加
      addTypeHandle (st) {
        // 通过传过来的st判断是添加还是修改
        if (st === 'add'){
          //添加进入这个
          window.parent.menuHandle({
            id: '2',
            url: '/backend/page/type/add.html',
            name: '添加图书类型'
          },true)
        } else {
          //修改进入这个
          window.parent.menuHandle({
            id: '2',
            //如果是修改st肯定就是id 带上参数传入下一个页面
            url: '/backend/page/type/add.html?id='+st,
            name: '修改图书类型'
          },true)
        }
      },
      //删除、批量删除
      deleteTypeHandle(type,id) {
        //这里会判断是批量删除还是单个删除，并且不勾选数据直接点删除就会走这个逻辑
        if (type === '批量' && id === null) {
          if (this.checkList.length === 0) {
            return this.$message.error('请选择删除对象')
          }
        }
        this.$confirm('确认删除该图书, 是否继续?', '确定删除', {
          'confirmButtonText': '确定',
          'cancelButtonText': '取消',
        }).then(() => {
          //真正执行删除方法
          deleteType(type === '批量' ? this.checkList.join(',') : id).then(res => {
            if (res.code === 1) {
              this.$message.success('删除成功！')
              this.handleTypeQuery();
            } else {
              this.$message.error(res.msg || '操作失败')
            }
          }).catch(err => {
            this.$message.error('请求出错了：' + err)
          })
        })
      },
      // 全部操作
      handleSelectionChange (val){
        let checkArr = []
        val.forEach((n) => {
          checkArr.push(n.id)
        })
        this.checkList = checkArr
      },
      handleSizeChange (val) {
        this.pageSize = val
        this.init()
      },
      handleCurrentChange (val) {
        this.page = val
        this.init()
      }
    }
  })
</script>
</body>
</html>